package com.njtoyo.taxi.admin.rest.controller.marketing;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.marketing.seckill.RuleListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.seckill.*;
import com.njtoyo.taxi.admin.service.marketing.*;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/marketing/seckill")
@Api(tags = "秒杀活动管理")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private SeckillAwardService seckillAwardService;

    @Autowired
    private SeckillRuleService seckillRuleService;

    @Autowired
    private SeckillPrizeService seckillPrizeService;

    @Autowired
    private SeckillRecordService seckillRecordService;

    @GetMapping("")
    @ApiOperation(value = "秒杀活动列表", notes = "权限：seckill")
    @ApiOperationSupport(order = 1)
    @PermissionScope(names = {"seckill"})
    public RestResult getList(@Valid SeckillListWrapper seckillListWrapper) {
        return seckillService.getList(seckillListWrapper);
    }

    @PostMapping("")
    @ApiOperation(value = "创建秒杀活动", notes = "权限：seckill_create")
    @ApiOperationSupport(order = 1)
    @PermissionScope(names = {"seckill_create"})
    public RestResult create(@Valid @RequestBody CreateWrapper createWrapper) {
        return seckillService.create(createWrapper);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除秒杀活动", notes = "权限：seckill_delete")
    @ApiOperationSupport(order = 1)
    @PermissionScope(names = {"seckill_delete"})
    public RestResult delete(@PathVariable("id") Long id) {
        return seckillService.delete(id);
    }

    @PatchMapping("/{id}/is_offline_update")
    @ApiOperation(value = "修改秒杀活动下线更新状态", notes = "权限：seckill_update")
    @ApiOperationSupport(order = 1)
    @PermissionScope(names = {"seckill_update"})
    public RestResult updateIsOfflineUpdate(@PathVariable("id") Long id, @Valid @RequestBody UpdateIsOffineUpdateSeckillWrapper updateIsOffineUpdateSeckillWrapper) {
        return seckillService.updateIsOfflineUpdate(id, updateIsOffineUpdateSeckillWrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改秒杀活动", notes = "权限：seckill_update")
    @ApiOperationSupport(order = 1)
    @PermissionScope(names = {"seckill_update"})
    public RestResult update(@PathVariable("id") Long id, @Valid @RequestBody UpdateSeckillWrapper updateSeckillWrapper) {
        return seckillService.update(id, updateSeckillWrapper);
    }

    @PatchMapping("/{activityId}/rule")
    @ApiOperation(value = "设置秒杀活动规则", notes = "权限：seckill_rule_set")
    @ApiOperationSupport(order = 2)
    @PermissionScope(names = {"seckill_rule"})
    public RestResult setRule(@PathVariable("activityId") Long activityId, @Valid @RequestBody SetRuleWrapper setRuleWrapper) {
        return seckillRuleService.setRule(activityId, setRuleWrapper);
    }

    @GetMapping("/{activityId}/rule")
    @ApiOperation(value = "获取秒杀活动规则", notes = "权限：seckill_rule")
    @ApiOperationSupport(order = 2)
    @PermissionScope(names = {"seckill_rule"})
    public RestResult<List<RuleListPresenter>> getRule(@PathVariable("activityId") Long activityId) {
        return seckillRuleService.getRule(activityId);
    }

    @DeleteMapping("/{activityId}/rule/{ruleId}")
    @ApiOperation(value = "删除秒杀活动规则", notes = "权限：seckill_rule_detele")
    @ApiOperationSupport(order = 2)
    @PermissionScope(names = {"seckill_rule"})
    public RestResult deleteRule(@PathVariable("activityId") Long activityId, @PathVariable("ruleId") Long ruleId) {
        return seckillRuleService.deleteRule(activityId, ruleId);
    }

    @GetMapping("/{activityId}/award")
    @ApiOperation(value = "秒杀活动奖项列表", notes = "权限：seckill_award")
    @ApiOperationSupport(order = 3)
    @PermissionScope(names = {"seckill_award"})
    public RestResult getAwardList(@PathVariable("activityId") Long activityId, @Valid AwardListWrapper awardListWrapper) {
        return seckillAwardService.getList(activityId, awardListWrapper);
    }

    @DeleteMapping("/{activityId}/award/{awardId}")
    @ApiOperation(value = "删除秒杀活动奖项", notes = "权限：seckill_award_delete")
    @ApiOperationSupport(order = 3)
    @PermissionScope(names = {"seckill_award_delete"})
    public RestResult deleteAward(@PathVariable("activityId") Long activityId, @PathVariable("awardId") Long awardId) {
        return seckillAwardService.delete(activityId, awardId);
    }

    @PostMapping("/award")
    @ApiOperation(value = "创建秒杀活动奖项", notes = "权限：seckill_award_create")
    @ApiOperationSupport(order = 3)
    @PermissionScope(names = {"seckill_award_create"})
    public RestResult createAward(@Valid @RequestBody CreateAwardWrapper createAwardWrapper) {
        return seckillAwardService.create(createAwardWrapper);
    }

    @PatchMapping("/award/{id}")
    @ApiOperation(value = "修改秒杀活动奖项", notes = "权限：seckill_award_update")
    @ApiOperationSupport(order = 3)
    @PermissionScope(names = {"seckill_award_update"})
    public RestResult updateAward(@PathVariable("id") Long id, @Valid @RequestBody UpdateAwardWrapper updateAwardWrapper) {
        return seckillAwardService.update(id, updateAwardWrapper);
    }

    @PatchMapping("/award/{id}/is_online")
    @ApiOperation(value = "修改秒杀活动奖项是否为线上", notes = "权限：seckill_award_update")
    @ApiOperationSupport(order = 3)
    @PermissionScope(names = {"seckill_award_update"})
    public RestResult updateAwardIsOnline(@PathVariable("id") Long id, @Valid @RequestBody UpdateAwardIsOnlineWrapper updateAwardWrapper) {
        return seckillAwardService.updateIsOnline(id, updateAwardWrapper);
    }

    @GetMapping("/award/{awardId}/prize")
    @ApiOperation(value = "获取奖品列表", notes = "权限：seckill_prize")
    @ApiOperationSupport(order = 4)
    @PermissionScope(names = {"seckill_prize"})
    public RestResult getPrizeList(@PathVariable("awardId") Long awardId, @Valid PrizeListWrapper prizeListWrapper) {
        return seckillPrizeService.getList(awardId, prizeListWrapper);
    }

    @PostMapping("/award/{awardId}/prize")
    @ApiOperation(value = "创建奖品", notes = "权限：seckill_prize_create")
    @ApiOperationSupport(order = 4)
    @PermissionScope(names = {"seckill_prize_create"})
    public RestResult createPrize(@PathVariable("awardId") Long awardId, @Valid @RequestBody CreatePrizeWrapper createPrizeWrapper) {
        return seckillPrizeService.create(awardId, createPrizeWrapper);
    }

    @PatchMapping("/prize/{prizeId}")
    @ApiOperation(value = "修改奖品", notes = "权限：seckill_prize_update")
    @ApiOperationSupport(order = 4)
    @PermissionScope(names = {"seckill_prize_update"})
    public RestResult updatePrize(@PathVariable("prizeId") Long prizeId, @Valid @RequestBody UpdatePrizeWrapper updatePrizeWrapper) {
        return seckillPrizeService.update(prizeId, updatePrizeWrapper);
    }

    @DeleteMapping("/prize/{prizeId}")
    @ApiOperation(value = "删除奖品", notes = "权限：seckill_prize_delete")
    @ApiOperationSupport(order = 4)
    @PermissionScope(names = {"seckill_prize_delete"})
    public RestResult deletePrize(@PathVariable("prizeId") Long prizeId) {
        return seckillPrizeService.delete(prizeId);
    }

    @GetMapping("/{activityId}/record")
    @ApiOperation(value = "获取活动参与列表", notes = "权限：seckill_record")
    @ApiOperationSupport(order = 5)
    @PermissionScope(names = {"seckill_record"})
    public RestResult getPrizeList(@PathVariable("activityId") Long activityId, @Valid RecordListWrapper recordListWrapper) {
        return seckillRecordService.getList(activityId, recordListWrapper);
    }

    @PatchMapping("/prize/{prizeId}/is_online")
    @ApiOperation(value = "修改奖品是否为线上", notes = "权限：seckill_prize_update")
    @ApiOperationSupport(order = 4)
    @PermissionScope(names = {"seckill_prize_update"})
    public RestResult updatePrizeIsOnline(@PathVariable("prizeId") Long prizeId, @Valid @RequestBody UpdatePrizeIsOnlineWrapper updatePrizeWrapper) {
        return seckillPrizeService.updateIsOnline(prizeId, updatePrizeWrapper);
    }

}
